package com.cooba.adapter;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.GameEvaluationBean;
import com.cooba.bean.NewsLoad;
import com.cooba.utils.ACache;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

public class GameEvaluationAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private final ACache acache;
    private Context context;
    private List<GameEvaluationBean.EvaluationInfo> list;

    public List<GameEvaluationBean.EvaluationInfo> getList() {
        return list;
    }

    public void setList(List<GameEvaluationBean.EvaluationInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addNewData(List<GameEvaluationBean.EvaluationInfo> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<GameEvaluationBean.EvaluationInfo> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public GameEvaluationAdapter(Context context, List<GameEvaluationBean.EvaluationInfo> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        acache = ACache.get(context);
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.game_evaluation_list_item1, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.tv_tittle);
            holder.author = (TextView) convertView.findViewById(R.id.tv_author);
            holder.date = (TextView) convertView.findViewById(R.id.tv_date);
            holder.up_num = (TextView) convertView.findViewById(R.id.tv_up_num);
            holder.reply_num= (TextView) convertView.findViewById(R.id.tv_reply_num);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_icon);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        String isRead = acache.getAsString("readed_eval_list_" + list.get(position).getId());
        holder.title.setText(Utils.replaceString(list.get(position).getTitle()));//标题
        holder.author.setText(list.get(position).getName());
        if (!TextUtils.isEmpty(isRead) && isRead.equals("true")) {
            holder.title.setTextColor(context.getResources().getColor(R.color.darkgray));
        } else {
            holder.title.setTextColor(context.getResources().getColor(R.color.black));
        }
        //Log.d("wei",list.get(position).getReply_num()+","+list.get(position).getDate()+","+list.get(position).getUp_num());
        holder.date.setText(list.get(position).getDate().substring(0,10));
        //holder.up_num.setText(list.get(position).getUp_num());
        //holder.reply_num.setText(list.get(position).getReply_num());
        bitmapUtils.display(holder.img, list.get(position).getIcon());


        return convertView;
    }


    class ViewHolder {
        TextView title;
        TextView author;
        TextView date;
        TextView up_num;
        TextView reply_num;
        ImageView img;

    }

}
